package com.zkzx.wlhy.adaptor.service.truck;


import com.alibaba.fastjson.JSON;
import com.zkzx.common.constants.SourceEnum;
import com.zkzx.common.exception.BaseException;
import com.zkzx.common.exception.ExceptionEnum;
import com.zkzx.common.global.Global;
import com.zkzx.common.truck.command.TruckCommand;
import com.zkzx.common.truck.constants.*;
import com.zkzx.common.truck.model.TruckCertificatesVO;
import com.zkzx.common.truck.model.TruckVO;
import com.zkzx.common.truck.vo.HeaderLicenseVO;
import com.zkzx.common.truck.vo.TailLicenseVO;
import com.zkzx.util.data.BigDecimalUtil;
import com.zkzx.util.data.CutStringUtil;
import com.zkzx.wlhy.adaptor.clinet.ResultVO;
import com.zkzx.wlhy.adaptor.clinet.truck.TruckClient;
import com.zkzx.wlhy.adaptor.controller.truck.param.TruckDTO;
import com.zkzx.wlhy.adaptor.controller.truck.vo.WlhyTruckVO;
import com.zkzx.wlhy.adaptor.service.code.WlhyCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/21/11:35
 * @Description:
 */

@Slf4j
@Service
public class TruckService {

    @Autowired
    private TruckClient truckClient;


    /**
     * 查询是否存在该车辆
     * @param  id  司机id
     *  @param headerLicense  车牌号
     */
    public TruckCommand getTruck(Integer id, String headerLicense){

        ResultVO<WlhyTruckVO> resultVO = truckClient.getTruck(headerLicense);

        TruckCommand truckCommand = new TruckCommand();
        truckCommand.setId(id);
        if (WlhyCode.TWO_HUNDRED.equals(resultVO.getCode())){
            truckCommand.setStatus(TruckStatusEnum.NO_SUBMIT.name());
            truckCommand.setSource(SourceEnum.ONESELF.name());
        }else if (WlhyCode.TWO_HUNDRED_AND_FOUR.equals(resultVO.getCode())){
            truckCommand.setStatus(TruckStatusEnum.VERIFY_SUCCESS.name());
            WlhyTruckVO wlhyTruckVO = resultVO.getData();
            truckCommand = assembleTruck(id, truckCommand, wlhyTruckVO);
        }else{
            throw new BaseException(ExceptionEnum.WH_DATA_ERROR.getCode(),ExceptionEnum.WH_DATA_ERROR.getMsg());
        }
        return truckCommand;

    }

    /**
     * 新增
     * @param  id  司机id
     *@param  companyThreeId  第三方企业id
     */

    public TruckCommand add(Integer id, String companyThreeId, TruckVO truckVO){

        // 提交直接判断网货有没有 如果有 则用网货打的 没有在继续录入
        TruckCommand truck = this.getTruck(id,truckVO.getHeaderLicenseVO().getLicence());
        if(!TruckStatusEnum.NO_SUBMIT.name().equals(truck.getStatus())){
            return truck;
        }


        TruckDTO truckDTO = new TruckDTO();
        truckDTO.setCompanyId(companyThreeId);
        truckDTO.setCarLength( BigDecimalUtil.getDivide(truckVO.getVolume(),Global.ONE_THOUSAND,Global.TWO));
        truckDTO.setCarCode(truckVO.getTypeVO().getName());
        truckDTO.setCarType(truckVO.getTypeVO().getValue());
        truckDTO.setDrivingLicense(truckVO.getTruckCertificatesVO().getDrivingMainPic());
        truckDTO.setDrivingLicenseBack(truckVO.getTruckCertificatesVO().getDrivingSubPic());
        truckDTO.setIssueDate(truckVO.getTruckCertificatesVO().getReleaseTime());
        truckDTO.setIssuingOrganizations(truckVO.getTruckCertificatesVO().getOffice());
        truckDTO.setLicensePlateColor(TruckColorEnum.valueOf(truckVO.getLicensecolor()).getValue());
        truckDTO.setPlateNumber(truckVO.getHeaderLicenseVO().getLicence());
        truckDTO.setRegisterDate(truckVO.getTruckCertificatesVO().getRegTime());
        truckDTO.setUseCharacter(OperationEnum.valueOf(truckVO.getNature()).getValue());
        truckDTO.setVehicleEnergyType(EnergyEnum.valueOf(truckVO.getEnergyType()).getCode());
        truckDTO.setVin(truckVO.getIdentify());

        truckDTO.setRoadTransportLicense(truckVO.getTruckCertificatesVO().getTransportPic());
        truckDTO.setRoadTransportLicenseCode(truckVO.getTruckCertificatesVO().getTransportNum());
        truckDTO.setRoadTransportLicenseEndDate(truckVO.getTruckCertificatesVO().getTransportValidTime());
        truckDTO.setTos(Global.ONE);

        ResultVO resultVO = truckClient.add(truckDTO);

        TruckCommand truckCommand = new TruckCommand();
        truckCommand.setId(id);
        if (WlhyCode.TWO_HUNDRED.equals(resultVO.getCode())){
             truckCommand.setThreeId(String.valueOf(resultVO.getData()));
             truckCommand.setSource(SourceEnum.ONESELF.name());
             truckCommand.setStatus(TruckStatusEnum.APPROVE_ING.name());
        }else if (WlhyCode.TWO_HUNDRED_AND_FOUR.equals(resultVO.getCode())){
            //添加数据在数据库已存在且状态为审核通过
            truckCommand.setStatus(TruckStatusEnum.VERIFY_SUCCESS.name());
            WlhyTruckVO wlhyTruckVO = JSON.parseObject(JSON.toJSONString(resultVO.getData()), WlhyTruckVO.class);
            truckCommand = assembleTruck(id, truckCommand, wlhyTruckVO);
        }else {
            throw new BaseException(ExceptionEnum.WH_DATA_ERROR.getCode(),ExceptionEnum.WH_DATA_ERROR.getMsg());
        }
        return truckCommand;
    }

    /**
     * 查询车辆审核状态
     * @param  id  司机id
     * @param threeId  车辆的三方id
     */

    public TruckCommand getStatus(Integer id, String threeId){


        ResultVO<String> resultVO = truckClient.getStatus(threeId);
        TruckCommand truckCommand = new TruckCommand();
        truckCommand.setId(id);

        if (WlhyCode.TWO_HUNDRED.equals(resultVO.getCode())){
            truckCommand.setStatus(TruckStatusEnum.VERIFY_SUCCESS.name());
        }else if (WlhyCode.TWO_HUNDRED_AND_FIVE.equals(resultVO.getCode())){
            truckCommand.setStatus(TruckStatusEnum.APPROVE_ING.name());
        }else if (WlhyCode.FOUR_HUNDRED_AND_ONE.equals(resultVO.getCode())){
            truckCommand.setStatus(TruckStatusEnum.APPROVE_FAIL.name());
        }else {
            //400、其他
            truckCommand.setStatus(TruckStatusEnum.APPROVE_FAIL.name());
            truckCommand.setRemark(resultVO.getMessage());
        }

        return truckCommand;

    }


    /**
     *组装车辆信息
     * @param id  司机id
     *  @param
     */
    public TruckCommand assembleTruck(Integer id, TruckCommand truckCommand, WlhyTruckVO wlhyTruckVO){
        truckCommand.setThreeId(wlhyTruckVO.getId());
        //truckCommand.setCode();
        truckCommand.setOwner(wlhyTruckVO.getTrueOwner());

        truckCommand.setHeaderLicenseVO(HeaderLicenseVO.builder()
                .province(CutStringUtil.getFirst(wlhyTruckVO.getPlateNumber()))
                .number(CutStringUtil.getSix(wlhyTruckVO.getPlateNumber()))
                .licence(wlhyTruckVO.getPlateNumber())
                .build());
        truckCommand.setTailLicenseVO(TailLicenseVO.builder()
                .province(CutStringUtil.getFirst(wlhyTruckVO.getTrailerVehiclePlateNumber()))
                .number(CutStringUtil.getSix(wlhyTruckVO.getTrailerVehiclePlateNumber()))
                .licence(wlhyTruckVO.getTrailerVehiclePlateNumber())
                .build());
        truckCommand.setLicensecolor(TruckColorEnum.getNameByValue(wlhyTruckVO.getLicensePlateColor()));
        truckCommand.setType(wlhyTruckVO.getCarCode());
        truckCommand.setEnergyType(EnergyEnum.getNameByCode(wlhyTruckVO.getVehicleEnergyType()));
        truckCommand.setVolume(BigDecimalUtil.getMultiple(wlhyTruckVO.getCarLength(), Global.ONE_THOUSAND));
        truckCommand.setTotalWeight(wlhyTruckVO.getGrossMass());
        truckCommand.setCurbWeight(wlhyTruckVO.getCarLoad());
        truckCommand.setVerificationWeight(wlhyTruckVO.getVehicleTonnage());
        truckCommand.setTowWeight(wlhyTruckVO.getTractiveTonnage());
        truckCommand.setIdentify(wlhyTruckVO.getVin());
        truckCommand.setNature(OperationEnum.getNameByValue(wlhyTruckVO.getUseCharacter()));
        truckCommand.setSource(SourceEnum.SHARE.name());


        TruckCertificatesVO truckCertificatesVO = new TruckCertificatesVO();
        truckCertificatesVO.setTruckId(id);
        truckCertificatesVO.setOffice(wlhyTruckVO.getIssuingOrganizations());
        truckCertificatesVO.setReleaseTime(wlhyTruckVO.getIssueDate());
        truckCertificatesVO.setRegTime(wlhyTruckVO.getRegisterDate());
        truckCertificatesVO.setDrivingMainPic(wlhyTruckVO.getDrivingLicense());
        truckCertificatesVO.setDrivingSubPic(wlhyTruckVO.getDrivingLicenseBack());
        truckCertificatesVO.setTransportNum(wlhyTruckVO.getRoadTransportLicenseCode());
        truckCertificatesVO.setTransportValidTime(wlhyTruckVO.getRoadTransportLicenseEndDate());
        truckCertificatesVO.setTransportPic(wlhyTruckVO.getRoadTransportLicense());

        truckCommand.setTruckCertificatesVO(truckCertificatesVO);
        return truckCommand;
    }

}
